package com.modern.js.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.modern.js.entity.Employee;
import com.modern.js.entity.Permission;
import com.modern.js.service.IPermissionService;
import com.modern.js.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author PIGS
 * @since 2020-03-23
 */
@RestController
@RequestMapping("/perm")
public class PermissionController {
    @Autowired
    private IPermissionService iPermissionService;

    /**
     *
     * @param page 当前页码
     * @param limit 每页的记录数
     * @return
     */
    @GetMapping("/list")
    public Map getList(Integer page, Integer limit,String permName,String createTime){
        Map<String,Object> map = new HashMap<String,Object>();
        Page<Permission> permissionPage = new Page<Permission>();
        permissionPage.setSize(limit);//设置每页记录数
        permissionPage.setCurrent(page);//设置当前页码
        IPage<Permission> iPage = iPermissionService.selectAll(permissionPage,permName,createTime);
        if (iPage.getTotal()!=0){
            map.put("msg","查询成功！");
        }else {
            map.put("msg","查询失败！");
        }
        map.put("data",iPage.getRecords());
        map.put("count",iPage.getTotal());
        map.put("code",0);
        return map;
    }

    @PostMapping("/add")
    public boolean addPerm(Permission permission){
        permission.setCreateTime(StringUtils.getNowTime());
        permission.setUpdateTime(StringUtils.getNowTime());
        permission.setIsDel(1);
        boolean result = iPermissionService.save(permission);
        return result;
    }

    @PutMapping("/update")
    public boolean updatePerm(Permission permission){
        permission.setUpdateTime(StringUtils.getNowTime());
        boolean result = iPermissionService.updateById(permission);
        return result;
    }
    @DeleteMapping("/del/{permId}")
    public boolean delPerm(@PathVariable Integer permId){
        Permission permission = new Permission();
        permission.setIsDel(0);
        permission.setPermId(permId);
        boolean result = iPermissionService.updateById(permission);
        return result;
    }
}
